package com.pzh.guli.service.edu.controller.admin;


import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.edu.pojo.Chapter;
import com.pzh.guli.service.edu.pojo.vo.ChapterVo;
import com.pzh.guli.service.edu.service.IChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@RestController
@RequestMapping("admin/edu/chapter")
public class ChapterController {
    @Autowired
    private IChapterService chapterService;
    //获取章节树
    @GetMapping("list/{courseId}")
    public Result chapterNestList(@PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestList(courseId);
        return Result.ok().data("items",chapterVoList);
    }
    //新增章节
    @PostMapping("save")
    public Result save(@RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if (save){
           return   Result.ok().message("添加成功");
        }
        return Result.error().message("添加失败");
    }
    //删除章节
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id){
        boolean remove = chapterService.removeById(id);
        if (remove){
            return  Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");
    }
    //根据id查chapter
    @GetMapping("/{id}")
    public Result getChapterById(@PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return Result.ok().data("item",chapter);
    }
    //更新chapter
    //新增章节
    @PutMapping("update")
    public Result update(@RequestBody Chapter chapter){
        boolean update = chapterService.updateById(chapter);
        if (update){
            return   Result.ok().message("更新成功");
        }
        return Result.error().message("更新失败");
    }
    //通过id删除章节
    @DeleteMapping("delete/{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId){
        boolean remove= chapterService.removeChapterById(chapterId);
        if (remove){
            return Result.ok().message("章节删除成功");
        }
        return Result.error().message("章节删除失败");
    }
}

